package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.SystemDictionaryItem;
import cn.wolfcode.rbac.mapper.SystemDictionaryItemMapper;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryItemServiceImpl implements ISystemDictionaryItemService {

    @Autowired
    private SystemDictionaryItemMapper systemDictionaryItemMapper;

    @Override
    public void delete(Long id) {
        systemDictionaryItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(SystemDictionaryItem systemDictionaryItem) {
        // 序号自增处理
        // 判断用户是否有填序号，如果没有填，就自动设置为当前目录下最大的明细序号
        if(systemDictionaryItem.getSequence() == null){
            int max = systemDictionaryItemMapper.selectMaxSequenceByparentId(systemDictionaryItem.getParentId());
            systemDictionaryItem.setSequence(max+1);
        }
        systemDictionaryItemMapper.insert(systemDictionaryItem);
    }

    @Override
    public SystemDictionaryItem get(Long id) {
        return systemDictionaryItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionaryItem> listAll() {
        return systemDictionaryItemMapper.selectAll();
    }

    @Override
    public void update(SystemDictionaryItem systemDictionaryItem) {
        systemDictionaryItemMapper.updateByPrimaryKey(systemDictionaryItem);
    }

    @Override
    public PageInfo query(QueryObject qo) {
        /*int count = systemDictionaryItemMapper.selectForCount(qo);
        // 判断是否有数据
        if(count == 0){
            return new PageResult(qo.getPageSize());
        }*/
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"sequence ASC");  // 返回页码和每页数量

        // 查询结果集
        List<SystemDictionaryItem> list = systemDictionaryItemMapper.selectForList(qo);
//        return new PageResult(qo.getCurrentPage(),count,list,qo.getPageSize());
        return new PageInfo(list);
    }

    @Override
    public List<SystemDictionaryItem> selecteByDiSn(String sn) {
        return systemDictionaryItemMapper.selecteByDiSn(sn);
    }
}
